package com.tsll.controller;

import com.github.pagehelper.util.StringUtil;
import com.tsll.entity.Staff;
import com.tsll.service.impl.StaffServiceImpl;
import com.tsll.vo.LoginVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private StaffServiceImpl staffService;
    @Autowired
    private  HttpSession session;
    /**
     * 处理用户登录请求。
     *
     *  loginVo 包含登录信息的Vo对象，其中包含用户名和密码。
     */
    @RequestMapping("/all")
    public Result login(@RequestBody LoginVo loginVo){
        // 检查用户名和密码是否为空
        if (StringUtil.isEmpty(loginVo.getUsername())||StringUtil.isEmpty(loginVo.getPassword())){
            return new Result(500,"账号密码为空",null);
        }
        // 调用staffService进行用户名和密码的验证
        Result result = staffService.selectname(loginVo.getUsername(),loginVo.getPassword());
        return result;
    }
   @RequestMapping("/ass/{oldpass}/{newpass}")
    public Result ss(@PathVariable String oldpass, @PathVariable String newpass){
       Staff staff1 = (Staff) session.getAttribute("staff");
       String password = staff1.getPassword();

       if (oldpass.equals(password)){
           staffService.updateLogin(newpass,staff1.getId());
           return new Result(200,"修改成功",null);
       }
       return new Result(500,"密码和旧密码不相同",null);
   }


   @RequestMapping("/exit")
    public  Result exit(){
        session.removeAttribute("staff");
        return new Result(200,"退出成功",null);
   }

}
